/**
 * Yolema.com Inc.
 * Copyright (c) 2011-2012 All Rights Reserved.
 */
package org.grayrabbit.pay.http;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.grayrabbit.pay.core.model.exception.PayException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 汇付发起收款、退款
 * 
 * @author wy
 * @version $Id: SendPay.java, v 0.1 2012-11-4 下午1:04:57 wy Exp $
 */
public class HttpUtil {

    private final static Logger LOG = LoggerFactory.getLogger(HttpUtil.class);

    /**
     * 发送请求
     * 
     * @param url
     * @param paramsMap
     * @return
     */
    public static HttpResponse httpPost(String url, Map<String, String> paramsMap) {

        HttpClient client = new DefaultHttpClient();

        HttpPost httpPost = new HttpPost(url);
        //表单参数
        List<NameValuePair> nvpsList = new ArrayList<NameValuePair>();

        if (paramsMap != null && !paramsMap.isEmpty()) {
            for (String key : paramsMap.keySet()) {
                nvpsList.add(new BasicNameValuePair(key, paramsMap.get(key)));
            }
        }

        try {
            //编码 汇付交易一定要UTF-8 要不会返回 41错误
            httpPost.setEntity(new UrlEncodedFormEntity(nvpsList, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            LOG.error("表单参数编码转换失败");
            throw new PayException("表单参数编码转换失败");
        }

        try {
            return client.execute(httpPost);
        } catch (ClientProtocolException e) {
            LOG.error("访问错误ClientProtocolException", e);
            throw new PayException("访问错误ClientProtocolException");
        } catch (IOException e) {
            LOG.error("访问错误IOException", e);
            throw new PayException("访问错误:IOException");
        } catch (Exception e) {
            LOG.error("访问错误", e);
            throw new PayException("访问错误:未知异常");
        }
    }

    /**
     * 返回结果转换为html
     * 
     * @param response
     * @return
     */
    public static String response2Html(HttpResponse response) {
        LOG.info("HTTP 状态:{}" + response.getStatusLine().getStatusCode());
        if (response.getStatusLine().getStatusCode() != 200) {
            throw new PayException("访问错误:" + response.getStatusLine());
        }
        try {
            return EntityUtils.toString(response.getEntity());
        } catch (ParseException e) {
            LOG.error("response ParseException", e);
            throw new PayException("response格式化异常");
        } catch (IOException e) {
            LOG.error("response IOException", e);
            throw new PayException("response文件流异常");
        }

    }

}
